package com.test.lucene;

import java.io.IOException;
import java.util.Date;

import com.test.lucene.helper.SharedParameters;
import com.test.lucene.index.DocumentCollection;



/**
 * Abstract manager class.
 * 
 * @author Patrick Kimber
 * 
 */
abstract class Manager {

    /** The number of milliseconds in a second. */
    private static final long MILLISECONDS_PER_SECOND = 1000;

    /** The number of seconds in a minute. */
    private static final long SECONDS_PER_MINUTE = 60;

    /** The start time. */
    private Date finishTime;

    /**
     * Constructor.
     * 
     * @param args
     *            Commmand line parameters.
     */
    protected Manager(final String[] args) {
        createParameters(args);
    }

    /**
     * Create the parameters for this class.
     * 
     * @param args
     *            command line parameters.
     */
    protected abstract void createParameters(final String[] args);

    /** @return the parameters. */
    protected abstract SharedParameters getSharedParameters();

    /**
     * Write documents to a Lucene repository.
     * 
     * @throws LuceneException
     *             If a Lucene operation fails.
     * @throws IOException
     *             If an IO operation fails.
     * @throws InterruptedException
     *             If the sleep is interrupted.
     */
    protected abstract void run() throws LuceneException, IOException,
            InterruptedException;

    /**
     * Create a Lucene document collection.
     * 
     * @return A Lucene document collection
     */
    protected final DocumentCollection createCollection() {
        return new DocumentCollection(getSharedParameters()
                .getRepositoryFolder());
    }

    /** @return The number of milliseconds to sleep between each request. */
    protected final long getDefaultSleep() {
        return getSharedParameters().getSleep();
    }

    /** @return the startTime. */
    protected final Date getFinishTime() {
        return finishTime;
    }

    /** Set the startTime. */
    protected final void setFinishTime() {
        long delay = getSharedParameters().getMinutes() * SECONDS_PER_MINUTE
                * MILLISECONDS_PER_SECOND;
        finishTime = new Date(new Date().getTime() + delay);
    }

    /**
     * Is it time to stop searching?
     * 
     * @return <code>true</code>if time to stop.
     */
    protected final boolean isFinished() {
        Date now = new Date();
        long millisecondsRemaining = getFinishTime().getTime() - now.getTime();
        System.out.println("Seconds remaining... " + millisecondsRemaining
                / MILLISECONDS_PER_SECOND);
        return now.after(getFinishTime());
    }

    /**
     * Are the parameters valid?
     * 
     * @return <code>true</code>if the parameters are valid.
     */
    protected boolean isValid() {
        return getSharedParameters().isValid();
    }
}
